//
//  MainViewController.h
//  RunSketch
//
//  Created by Brian Hinch on 7/8/08.
//  Copyright Tellart 2008. All rights reserved.
//

#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#import "SketchWebView.h"
#import "Socket.h"
#import "RootViewController.h"

#ifdef __cplusplus 
#import "AM_IO.h"
#endif

#define kAccelerometerPeriod            0.040f
#define kSketchServerPeriod             0.080f
#define kSketchServerPeriodBoosted      0.030f
#define kLoopsPerPollForIOPackOutput	10
#define kSketchIOPackOutPollFcn         @"skc_poll"
#define kSketchCallbackOnIOPack         @"sketchOnIOPack"
#define kSketchCallbackOnAccelerate     @"sketchOnAccelerate"
#define kSketchCallbackOnLocation       @"sketchOnLocation"
#define kSketchCallbackOnAudioAmplitude @"sketchOnAudioAmplitude"
#define kFlowControlWaitForSetTimeout   1.0f

@class SketchUpdateController;

@interface MainViewController : UIViewController <UIAccelerometerDelegate, UIWebViewDelegate, CLLocationManagerDelegate> {
	SketchWebView *webView;
	CLLocationManager *locationManager;
    
	int				skc_loopCount;
	
	//to use timer looping, uncomment the next line
    NSTimer         *skc_socketTimer;
	
    Socket          *skc_socket;
    BOOL            skc_socketIsConnecting;
    //NSMutableData   *skc_inData;
	int				skc_socket_readBufferCursor;
	int				skc_socket_numLastBytesRead;
    int             skc_incomingMessageBufferCursor;
    int             skc_incomingMessageBufferExpectedLength;
    unsigned int	skc_incomingMessageBufferSize;
    //void            *skc_incomingMessageBuffer;
	char            *skc_incomingMessageBuffer;
    //void            *skc_outgoingMessageBuffer;
	char            *skc_outgoingMessageBuffer;
    unsigned int	skc_outgoingMessageBufferSize;

	/*
	 * network io optimization variables
	 */
	int	cin;
    int	last_inVal0;
    int	last_inVal1;
    int last_inVal2;
    int last_inVal3;
    int	last_outVal0;
    int	last_outVal1;
    int last_outVal2;
    int last_outVal3;
    int	inVal0;
    int	inVal1;
    int inVal2;
    int inVal3;
    int	outVal0;
    int	outVal1;
    int outVal2;
    int outVal3;
	
	CFAbsoluteTime	now;
	CFAbsoluteTime	lastFireTime;
	CFAbsoluteTime	lastIOPackSampleTime;
	float			actualIOPackSamplingRate;
	
	NSString		*retainedJavaScriptCall;
	NSString		*retainedSendValuesStr;
	NSScanner		*skc_sketchPollingScanner;
    
    int socketBufferParseCursor;
    int sensorAccelerometerEnabled;
    int sensorLocationEnabled;
    int sketchServerEnabled;
    
    NSString        *sketchServerName;
	int serverFlowControlEnabled;
	CFAbsoluteTime serverFlowControlWaitForSetUntil;
	
	int iopackBoostEnabled;
#ifdef __cplusplus 
    AM_IO *am_io;
#endif
}
#ifdef __cplusplus 
@property AM_IO *am_io;
#endif

@property (nonatomic, retain) SketchWebView *webView;
- (void) closeDown;
- (void) initWebView;
- (void) loadSketch;
- (void) enableSensors;
- (void) disableSensors;
- (void) setSketchServerName: (NSString *)name;
- (id) getSketchServerName;
- (void) setFlowControlState: (int)state;
- (int) getFlowControlState;
- (void) setIOPackBoostState: (int)state;
- (int) getIOPackBoostState;
- (void) skc_sendSetMessage;
- (void) skc_sendGetMessage;
- (void)skc_allocMessageBuffer;
- (void)disableSensorLocation;
- (void)disableSensorAccelerometer;
- (void)disableSketchServer;
- (void)enableSensorLocation;
- (void)enableSensorAccelerometer;
- (void)enableSketchServer;
- (void) skc_startLoop;
- (void)skc_startLoopOnTimer;
- (void) skc_stopLoopOnTimer;
- (void)skc_loop;
- (void) skc_processInputBuffer;
- (void) skc_pollSketchForIOPackOutput_withScanner;
- (void) skc_decodeSetMessageFromBuffer;
- (void)audioAmplitudeTimer:(NSTimer*)timer;
- (void) audioAmplitude:(UInt8)amp;

@end
